/*
cd /projects/hsieh_project/proj_201809/code_2_201910/
qstata --dofile=out_mgrow2_1_c_emp.do --statatype=mp --cpucount=5 &

Generate variables of city employment in selected years and Sato-Vartia weight for later use
*/

set linesize 255
capture log close
//log using /projects/programs/flin/201809/201910_main/out_city_growth_log, replace text

clear all
capture noi program drop _all
cd /projects/

di "Started at $S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/201910_main/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/201910_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/city_mgrow2/"
capture mkdir ${dir_outf}
capture mkdir ${dir_outf}/data/

global gl_year = "1977 1982 1987 1992 1997 2002 2007 2013"
global gl_geo = "msa fips czone msa1983 msa1983cz"

//==============================================================================
// Function that calculates SV weight

local i_city = "msa"

capture program drop p_sv
program p_sv
	args year1 year2 i_city ds_out
	
	di "`year1' `year2'"
	
	preserve
	keep if inlist(year, `year1', `year2')
	drop year emp_cind emp_city
	duplicates drop
	
	/*
	----------------------------------------------------------------------------
	Generates city or city-ind employment in year1 and year2
	*/
	gen emp_city = emp_city_`year1'
	gen f_emp_city = emp_city_`year2'
	gen emp_cind = emp_cind_`year1'
	gen f_emp_cind = emp_cind_`year2'
	keep city ch_ind emp_city f_emp_city emp_cind f_emp_cind emp_city_*
	gen syear1 = `year1'
	gen syear2 = `year2'
	
	/*
	----------------------------------------------------------------------------
	Mark type of city-industry.
	We assign SV weight differently to incumbent city-ind and new city-ind.
	*/
	gen i_sato = .
	replace i_sato = 1 if emp_cind!=. & f_emp_cind!=. // incumbent city-industry
	replace i_sato = 2 if emp_cind==. & f_emp_cind!=. // new city-industry
	
	/* Incumbent city-ind */
	gen sv_num = .
	replace sv_num = (f_emp_cind - emp_cind) / (ln(f_emp_cind) - ln(emp_cind)) if i_sato == 1
	replace sv_num = f_emp_cind if i_sato == 1 & f_emp_cind == emp_cind
	
	sort city
	by city: egen sv_den = total(sv_num)
	gen w_sv = sv_num / sv_den
	
	/* New city-ind */
	replace w_sv = 1 if i_sato == 2
	
	/*
	----------------------------------------------------------------------------
	Append and save
	*/
	gen `i_city' = city
	order syear1 syear2 city `i_city', first
	
	append using `ds_out'
	save `ds_out', replace
	restore
end

//==============================================================================
// Loop over definition of cities

foreach i_city in $gl_geo {

di "`i_city'"

use "${dir_data}/cityind_sum_`i_city'_all", clear
rename `i_city' city
keep if inlist(year, 1977, 1982, 1987, 1992, 1997, 2002, 2007, 2013)
keep year city ch_ind emp_cind

/*
--------------------------------------------------------------------------------
Calculate and generate a variable of city employment for each year
*/
sort year city ch_ind
by year city: egen emp_city = total(emp_cind)

sort city year
foreach i_year in $gl_year {
	capture drop emp_city_y
	gen emp_city_y = emp_city if year == `i_year'
	by city: egen emp_city_`i_year' = mean(emp_city_y) // city emp in i_year
	capture drop emp_city_y
}

/*
--------------------------------------------------------------------------------
Calculate and generate a variable of city-ind employment for each year
*/
sort city ch_ind year
foreach i_year in $gl_year {
	capture drop emp_city_y
	gen emp_cind_y = emp_cind if year == `i_year'
	by city ch_ind: egen emp_cind_`i_year' = mean(emp_cind_y) // city-ind emp in i_year
	capture drop emp_cind_y
}

local ds_out = "${dir_outf}/data/cmgrow_sv_`i_city'"
preserve
clear
save `ds_out', replace emptyok
restore

p_sv 1977 1982 "`i_city'" "`ds_out'"
p_sv 1982 1987 "`i_city'" "`ds_out'"
p_sv 1987 1992 "`i_city'" "`ds_out'"
p_sv 1992 1997 "`i_city'" "`ds_out'"
p_sv 1997 2002 "`i_city'" "`ds_out'"
p_sv 2002 2007 "`i_city'" "`ds_out'"
p_sv 2007 2013 "`i_city'" "`ds_out'"

}

di "Ended at $S_DATE $S_TIME"
// End of do file
